package com.bestom.t9.utils;

public class BinaryDataConversion {

    //将bit位数值由1变为0，传入locate值要相差8位，如1 --> 9 ; 14 --> 6
    public short computRegOpenData(short data,int locate){

        //统一新增此if模块调8位
        if (locate >= 8) {
            locate -= 8;
        } else {
            locate += 8;
        }

        if (locate==0){
            return  (short)(data & 0xFFFE);
        }else if (locate==1){
            return  (short)(data & 0xFFFD);
        }else if (locate==2){
            return  (short)(data & 0xFFFB);
        }else if (locate==3){
            return  (short)(data & 0xFFF7);
        }else if (locate==4){
            return  (short)(data & 0xFFEF);
        }else if (locate==5){
            return  (short)(data & 0xFFDF);
        }else if (locate==6){
            return  (short)(data & 0xFFBF);
        }else if (locate==7){
            return  (short)(data & 0xFF7F);
        }else if (locate==8){
            return  (short)(data & 0xFEFF);
        }else if (locate==9){
            return  (short)(data & 0xFDFF);
        }else if (locate==10){
            return  (short)(data & 0xFBFF);
        }else if (locate==11){
            return  (short)(data & 0xF7FF);
        }else if (locate==12){
            return  (short)(data & 0xEFFF);
        }else if (locate==13){
            return  (short)(data & 0xDFFF);
        }else if (locate==14){
            return  (short)(data & 0xBFFF);
        }else if (locate==15){
            return  (short)(data & 0x7FFF);
        }
        return data;
    }

    //将bit位数值由0变为1，对应位传入locate值
    public short computRegData(short data,int locate){
        if (locate==0){
            return  (short)(data | 0x0100);
        }else if (locate==1){
            return  (short)(data | 0x0200);
        }else if (locate==2){
            return  (short)(data | 0x0400);
        }else if (locate==3){
            return  (short)(data | 0x0800);
        }else if (locate==4){
            return  (short)(data | 0x1000);
        }else if (locate==5){
            return  (short)(data | 0x2000);
        }else if (locate==6){
            return  (short)(data | 0x4000);
        }else if (locate==7){
            return  (short)(data | 0x8000);
        }else if (locate==8){
            return  (short)(data | 0x0001);
        }else if (locate==9){
            return  (short)(data | 0x0002);
        }else if (locate==10){
            return  (short)(data | 0x0004);
        }else if (locate==11){
            return  (short)(data | 0x0008);
        }else if (locate==12){
            return  (short)(data | 0x0010);
        }else if (locate==13){
            return  (short)(data | 0x0020);
        }else if (locate==14){
            return  (short)(data | 0x0040);
        }else if (locate==15){
            return  (short)(data | 0x0080);
        }
        return data;
    }


    public int computRegOpenByteData(int data,int locate){

        if (locate==0){
            return  (data & 0x7F);
        }else if (locate==1){
            return  (data & 0xBF);
        }else if (locate==2){
            return  (data & 0xDF);
        }else if (locate==3){
            return  (data & 0xEF);
        }else if (locate==4){
            return  (data & 0xF7);
        }else if (locate==5){
            return  (data & 0xFB);
        }else if (locate==6){
            return  (data & 0xFD);
        }else if (locate==7){
            return  (data & 0xFE);
        }
        return data;
    }

    public int computRegByteData(int data, int locate){
        if (locate==0){
            return  (data | 0x80);
        }else if (locate==1){
            return  (data | 0x40);
        }else if (locate==2){
            return  (data | 0x20);
        }else if (locate==3){
            return  (data | 0x10);
        }else if (locate==4){
            return  (data | 0x08);
        }else if (locate==5){
            return  (data | 0x04);
        }else if (locate==6){
            return  (data | 0x02);
        }else if (locate==7){
            return  (data | 0x01);
        }
        return data;
    }



    public String makeChecksum(String hexdata) {
        if (hexdata == null || hexdata.equals("")) {
            return "00";
        }
        hexdata = hexdata.replaceAll(" ", "");
        int total = 0;
        int len = hexdata.length();
        if (len % 2 != 0) {
            return "00";
        }
        int num = 0;
        while (num < len) {
            String s = hexdata.substring(num, num + 2);
            total += Integer.parseInt(s, 16);
            num = num + 2;
        }
        return hexInt(total);
    }

    private String hexInt(int total) {
        int a = total / 256;
        int b = total % 256;
        if (a > 255) {
            return hexInt(a) + format(b);
        }
        return format(a) + format(b);
    }

    private String format(int hex) {
        String hexa = Integer.toHexString(hex);
        int len = hexa.length();
        if (len < 2) {
            hexa = "0" + hexa;
        }
        return hexa;
    }
}
